<?php
namespace Admin\Model;
use Think\Model;
/**
 * 查账管理
 *
 */

class PayrecordModel extends Model{

	protected $tableName = 'pay_record';

	const AUDIT_NEED = 0;/* 待审核 */
	const AUDIT_OK = 1;
	const AUDIT_ERR = 2; /* 审核失败 */

	const PAY_UNKOWN = 0; /*未知*/
	const PYA_NEW = 1; /*新单*/
	const PAY_XUFEI =2;/*续费*/



	/*添加新单查账*/
	public function add_new(){
		$uinfo = UserModel::loadFromSession();
		//验证用户唯一
		$r = array('d'=>NULL,'i'=>'','r'=>0);
 		if( !$this->creator ){
 			$this->creator = $uinfo['user_id'];
			$this->creator_role = $uinfo['role_id'];
			$this->creator_sector = $uinfo['sector_id'];
 		}
 		$this->create_time = date('Y-m-d H:i:s');
 		$this->audit_res = self::AUDIT_NEED;
 		$this->pay_type = self::PYA_NEW;
 		$this->pay_mount = doubleval( $this->pay_mount );

 		//参数验证
 		if( !$this->customer_name || !$this->customer_phone || !$this->customer_mount || !$this->customer_source ){
 			$r['i'] = "客户信息填写不完整";
 			return $r;
 		}
		if( !$this->customer_product || !$this->customer_price || !$this->contract_start_time || !$this->customer_buycount){
			$r['i'] = "产品订购信息不完整";
			return $r;
		}

		if( !$this->pay_name || !$this->pay_mount || !$this->pay_way || !$this->pay_time ){
			$r['i'] = "付款信息不完整";
			return $r;
		}

 		//客户唯一性验证
		$wheres = array();
		$wheres['customer_phone'] = array('eq', $this->customer_phone );
		$find = NULL;
		$find = $this->where( $wheres )->find();
		if( $find ){
			$r['i'] = "该电话已经被别人使用";
			$r['d'] = $find;
			return $r;
		}

		$MCustomer = M('Customer');
		$wheres = array();
		$wheres['phone'] = array('eq', $this->customer_phone);
		$find = $MCustomer->where( $wheres )->find();
		if( $find ){
			$r['i'] = "该电话号码已经被别人注册";
			$r['d'] = $find;
			return $r;
		}

		/*QQ号码唯一性验证*/
		if( $this->customer_qq ){
			$wheres = array();
			$wheres['customer_qq'] = array('eq', $this->customer_qq );
			$find = $this->where( $wheres )->find();
			if( $find ){
				$r['i'] = "该QQ号码已经被别人使用";
				$r['d'] = $find;
				return $r;
			}

			$wheres = array();
			$wheres['qq'] = array('eq', $this->customer_qq );
			$find = $MCustomer->where( $wheres )->find();
			if( $find ){
				$r['i'] = "该QQ号码已经被别人注册";
				$r['d'] = $find;
				return $r;
			}
		}

		/*微信ID唯一性验证*/
		if( $this->customer_weixin_id ){
			$wheres = array();
			$wheres['customer_weixin_id'] = array('eq', $this->customer_weixin_id );
			$find = $this->where( $wheres )->find();
			if( $find ){
				$r['i'] = "该微信ID已经被别人使用";
				$r['d'] = $find;
				return $r;
			}

			$wheres = array();
			$wheres['weixin_id'] = array('eq', $this->customer_weixin_id);
			$find = $this->where( $wheres )->find();
			if( $find ){
				$r['i'] = "该微信ID已经被别人注册";
				$r['d'] = $find;
				return $r;
			}
		}

		$did = $this->add();
		if( $did ){
			$r['i'] = "OK";
			$r['d'] = $did;
			$r['r'] = 1;
		}else{
			$r['i'] = "添加新单查账失败,ERROR:".$this->getError();
			$r['d'] = mysql_error();
		}

		return $r;
	}

	public function Guid(){
		$uinfo = UserModel::loadFromSession();
		$userId = $uinfo['user_id'];
		return $userId;
	}

	public function gotapp(){
		$r = array('data'=>NULL,'msg'=>'','rcode'=>0);
		$wheres = $arr = $att = $ass = $amm = array();
		$uinfo = UserModel::loadFromSession();
		$sector = M('sector');
		$user = M('user');
		$wheres['sector_id'] = $uinfo['sector_id'];
		$rcompany = $sector->where($wheres)->find();
		$rback = $this->Guid();
		if ($rback == 1) {
		     $res = $user ->distinct(true)->field('nickname,user_id')->select();
		}else{
			$arr['parent_company'] = $rcompany['parent_company'];
			$res = $sector ->field('sector_id')
						   ->where($arr)
						   ->select();
			foreach ($res as $k => $v) {
				$att[] = $v['sector_id'];
			}
			$ass['sector_id'] = array('IN',$att);
			$res = $user ->distinct(true)->field('nickname,user_id')
										 ->where($ass)
										 ->select();
		}
		return $res;
	}

	// 审核验证
	public function verifyOK($arrArr = array('id'=>NULL,'pay_type'=>NULL,'audit_msg'=>NULL),$format='Y-m-d H:i:s'){
		$r = array('data'=>NULL,'msg'=>'','rcode'=>0,'vty'=>0);
		$arr = $wheres = $att = $att_ = $arr_ = $wheres_ = $amm_ = array();
		$Reuser = M('user') ->where(array('user_id'=> $this->Guid()))->find();
		if ($arrArr['pay_type']!= NULL && $arrArr['pay_type'] != 2) {
			$payres = $this->join('user ON payrecord.customer_sales = user.user_id')
					   		->where(array('payrecord_id'=>$arrArr['id']))
					   		->find();
		}else{
			$J_Res = $this->where(array('payrecord_id'=>$arrArr['id']))->find();
			if ($J_Res['contract_id']){//续费查账当有合同的时候
				$payres = $this->field('a.*,b.*,d.*,c.state as ConState,c.contract_id,c.customer_id,c.customer_sector,c.product_id,c.price_id,c.sales_person,c.sales_sector,c.mount,c.start_time,c.end_time,c.content,c.note,c.address,c.create_time,c.is_sign,c.is_print,c.is_recycle,c.is_delete')
							   ->table('payrecord as a')
							   ->join('customer_xufei as b ON b.customer_id = a.customer_id')
							   // ->join('user as c ON c.user_id = b.user_id')
							   ->join('contract as c ON c.contract_id = a.contract_id')
							   ->join('customer as d ON d.customer_id = b.customer_id')
							   ->where(array('a.payrecord_id'=>$arrArr['id']))
							   ->find();

				if ($payres['constate'] == -1 || $payres['constate']== 2 || $payres['constate'] == 3) {
					$attArr = $arrARR = array();
					$attArr['customer_sector']=$payres['sector_id'];
					$attArr['product_id']=$payres['customer_product'];
					$attArr['price_id']=$payres['customer_price'];
					$attArr['sales_person']=$payres['customer_sales'];
					$attArr['sales_sector']=$payres['customer_sector'];
					$attArr['customer_sales'] = $payres['user_id'];
					$attArr['customer_sector'] = $payres['user_sector'];
					$attArr['create_time']=date($format);
					$attArr['address']=$payres['customer_address'];
					$attArr['state']= 1;
					$service = M('product_price')->where(array('price_id'=>$payres['customer_price']))->find();
					$free_time = $service['free_time'];
					$attArr['mount']=$service['service_price'];
					if($service['service_time']=="12m"){
						 $time_ = $free_time + '12';
					}elseif($service['service_time']=="6m"){
						 $time_ = $free_time + '6';
					}elseif($service['service_time']=="4m"){
						$time_ = $free_time + '4';
					}else{
						$time_ = $free_time + '1';
					}
					$e_time = date("Y-m-d",strtotime("+".$time_." months",strtotime($payres['contract_start_time'])));
					$attArr['end_time'] = $e_time;
					// $rback = M('customer_contact_log')->where(array('customer_id'=>$payres['customer_id']))->count();
					// $attArr['count_contact']=$rback;
					$attArr['customer_id'] = $payres['customer_id'];
				    $attArr['start_time']=$payres['contract_start_time'];
					$conadd = M('contract')->add($attArr);
					if (!empty($conadd)) {
						$arrARR['contract_id'] = $conadd;
					}else{
						$r['msg'] = '合同信息生成失败';
					}
					// 修改payrecord
					$arrARR['auditor']=$this->Guid();
					$arrARR['audit_sector']=$Reuser['sector_id'];
					$arrARR['audit_role'] = $Reuser['role_id'];
					$arrARR['payrecord_id'] = $arrArr['id'];
					if ($arrArr['audit_msg']) {
						$arrARR['audit_msg'] = $arrArr['audit_msg'];
					}
					$arrARR['audit_time']=date($format);
					$arrARR['audit_res'] = 1;
					$paysave = $this->save($arrARR);
					if ($paysave) {
						$r['rcode']=1;
						$r['msg'] = '审核成功';
						$r['data'] = $this->where(array('payrecord_id'=>$arrArr['id']))->find();
					}else{
						$r['msg'] = '请重新评论审核信息';
					}
					return $r;
				}
			}else{//续费查账当是直接添加用户的时候
				$payres = $this->field('a.*,b.*,d.*')
							   ->table('payrecord as a')
							   ->join('customer_xufei as b ON b.customer_id = a.customer_id')
							   // ->join('user as c ON c.user_id = b.user_id')
							   ->join('customer as d ON d.customer_id = b.customer_id')
							   ->where(array('a.payrecord_id'=>$arrArr['id']))
							   ->find();
			}

		}
		if ($payres) {
			if ($arrArr['pay_type'] == 2 ) {//添加合同
				if (!$payres['contract_id']) {
					$attArr = $arrARR = array();
					$attArr['customer_sector']=$payres['sector_id'];
					$attArr['product_id']=$payres['customer_product'];
					$attArr['price_id']=$payres['customer_price'];
					$attArr['sales_person']=$payres['customer_sales'];
					$attArr['sales_sector']=$payres['customer_sector'];
					$attArr['customer_sales'] = $payres['user_id'];
					$attArr['customer_sector'] = $payres['user_sector'];
					$attArr['create_time']=date($format);
					$attArr['address']=$payres['customer_address'];
					$attArr['state']= 1;
					$service = M('product_price')->where(array('price_id'=>$payres['customer_price']))->find();
					$free_time = $service['free_time'];
					$attArr['mount']=$service['service_price'];
					if($service['service_time']=="12m"){
						 $time_ = $free_time + '12';
					}elseif($service['service_time']=="6m"){
						 $time_ = $free_time + '6';
					}elseif($service['service_time']=="4m"){
						$time_ = $free_time + '4';
					}else{
						$time_ = $free_time + '1';
					}
					$e_time = date("Y-m-d",strtotime("+".$time_." months",strtotime($payres['contract_start_time'])));
					$attArr['end_time'] = $e_time;
					// $rback = M('customer_contact_log')->where(array('customer_id'=>$payres['customer_id']))->count();
					// $attArr['count_contact']=$rback;
					$attArr['customer_id'] = $payres['customer_id'];
				    $attArr['start_time']=$payres['contract_start_time'];
					$conadd = M('contract')->add($attArr);
					if (!empty($conadd)) {
						$arrARR['contract_id'] = $conadd;
					}else{
						$r['msg'] = '合同信息生成失败';
					}
					// 修改payrecord
					$arrARR['auditor']=$this->Guid();
					$arrARR['audit_sector']=$Reuser['sector_id'];
					$arrARR['audit_role'] = $Reuser['role_id'];
					$arrARR['payrecord_id'] = $arrArr['id'];
					if ($arrArr['audit_msg']) {
						$arrARR['audit_msg'] = $arrArr['audit_msg'];
					}
					$arrARR['audit_time']=date($format);
					$arrARR['audit_res'] = 1;
					$paysave = $this->save($arrARR);
					if ($paysave) {
						$r['rcode']=1;
						$r['msg'] = '审核成功';
						$r['data'] = $this->where(array('payrecord_id'=>$arrArr['id']))->find();
					}else{
						$r['msg'] = '请重新评论审核信息';
					}
				}else{
					$cont = array();
					if ($payres['customer_product'] == $payres['product_id']) {
						$cont['contract_id'] = $payres['contract_id'];
						$r['vty'] = 1;
						$att_['price_id']= $payres['customer_price'];
						M('contract')->startTrans();
						$service = M('product_price')->where($att_)->find();
						$free_time = $service['free_time'];
						if($service['service_time']=="12m"){
							 $time_ = $free_time + '12';
						}elseif($service['service_time']=="6m"){
							 $time_ = $free_time + '6';
						}elseif($service['service_time']=="4m"){
							$time_ = $free_time + '4';
						}else{
							$time_ = $free_time + '1';
						}
						$e_time = date("Y-m-d",strtotime("+".$time_." months",strtotime($payres['end_time'])));
						$cont['end_time'] = $e_time;
						$con_add = M('contract')->save($cont);
						if ($con_add) {
							M('contract')->commit();
						}else{
							$r['msg'] = '合同信息修改失败';
							return $r;
						}
					}
					$this->startTrans();
					$arr['auditor']=$this->Guid();
					$arr['audit_sector']=$Reuser['sector_id'];
					$arr['audit_role'] = $Reuser['role_id'];
					$arr['payrecord_id'] = $arrArr['id'];
					if ($arrArr['audit_msg']) {
						$arr['audit_msg'] = $arrArr['audit_msg'];
					}
					$arr['audit_time']=date($format);
					$arr['audit_res'] = 1;
					$arr['customer_sales'] = $payres['user_id'];
					$arr['customer_sector'] = $payres['user_sector'];
					$paysave = $this->save($arr);
					if ($paysave) {
						$this->commit();
					}
					if ($paysave) {
						$r['rcode']=1;
						if ($payres['customer_product'] == $payres['product_id']){
							$r['msg'] = '审核成功';
						}else{
							$r['msg'] = '所选的产品与合同产品不一致';
						}
						$r['data'] = $this->where(array('payrecord_id'=>$arrArr['id']))->find();
					}else{
						M('contract')->rollback();
						$this->rollback();
						$r['msg'] = '请重新评论审核信息';
					}
				}
			}else{
				// 添加customer表
				M('customer')->startTrans();
				$wheres['login_name']= $payres['customer_name'];
				$wheres['nickname']=$payres['customer_name'];
				$wheres['phone']=$payres['customer_phone'];
				$ctime = date($format);
				$wheres['pass']=md6("hzhfzx".$ctime);
				$wheres['qq']=$payres['customer_qq'];
				$wheres['weixin_id']=$payres['customer_weixin_id'];
				$amm['price_id']=$payres['customer_price'];
				$wheres['mount']=$payres['customer_mount'];
				$wheres['sex'] = $payres['customer_sex'];
				$wheres['career']= $payres['customer_career'];
				$wheres['birthday']=$payres['customer_birth'];
				$wheres['education']=$payres['customer_education'];
				$wheres['source_id'] = $payres['customer_source'];
				$wheres['note'] = $payres['customer_note'];
				$wheres['location'] = $payres['customer_location'];
				$wheres['address']= $payres['customer_address'];
				$wheres['sales_sector']=$payres['sector_id'];
				$wheres['sales_role']=$payres['role_id'];
				$wheres['state']=1;
				$wheres['sales_person']=$payres['customer_sales'];
				$wheres['sector_id']=$payres['customer_sector'];
				$wheres['create_time'] = date($format);
				$wheres['last_time']=date($format);
				$wheres['last_ip']= get_client_ip();
				$addcom = M('customer')->add($wheres);
				if ($addcom) {
					$arr['customer_id']=$addcom;
					$att['customer_id']=$addcom;
					$arr_['customer_id']=$addcom;
					$wheres_['customer_id']=$addcom;
					M('customer')->commit();
				}
				// 添加contract表
				M('contract')->startTrans();
				$att['customer_sector']=$payres['customer_sector'];
				$att['product_id']=$payres['customer_product'];
				$att['price_id']=$payres['customer_price'];
				$att['sales_person']=$payres['customer_sales'];
				$att['sales_sector']=$payres['customer_sector'];
				$att['create_time']=date($format);
				$att['address']=$payres['customer_address'];
				$att['state']= 1;

				$att_['price_id']= $payres['customer_price'];
				$service = M('product_price')->where($att_)->find();
				$att['mount']=$service['service_price'];
				$free_time = $service['free_time'];
				if($service['service_time']=="12m"){
					 $time_ = $free_time + '12';
				}elseif($service['service_time']=="6m"){
					 $time_ = $free_time + '6';
				}elseif($service['service_time']=="4m"){
					$time_ = $free_time + '4';
				}else{
					$time_ = $free_time + '1';
				}
				$e_time = date("Y-m-d",strtotime("+".$time_." months",strtotime($payres['contract_start_time'])));
				$att['end_time'] = $e_time;

				$rback = M('customer_contact_log')->where($arr_)->count();
				$att['count_contact']=$rback;


			    $att['start_time']=$payres['contract_start_time'];
				$conadd = M('contract')->add($att);
				if ($conadd) {
					$arr['contract_id']=$conadd;
					M('contract')->commit();
				}

				// 修改payrecord表
				$this->startTrans();
				$arr['auditor']=$this->Guid();
				$arr['audit_sector']=$Reuser['sector_id'];
				$arr['audit_role'] = $Reuser['role_id'];
				$arr['payrecord_id'] = $arrArr['id'];
				if ($arrArr['audit_msg']) {
					$arr['audit_msg'] = $arrArr['audit_msg'];
				}
				$arr['audit_time']=date($format);
				$arr['audit_res'] = 1;
				$paysave = $this->save($arr);
				if ($paysave) {
					$this->commit();
				}

				// 添加customer_xufei表, 只有在新单查账的时候才用填写的表,在续费审核和退款审核中是不用填写的
				M('customer_xufei')->startTrans();
				// $wheres_['customer_id'] = $payres['customer_id'];
				$wheres_['user_id'] = $payres['customer_sales'];
				$wheres_['user_sector']= $payres['sector_id'];
				$wheres_['user_role'] = $payres['role_id'];
				$wheres_['is_new'] =  1;
				$wheres_['customer_type'] = 1;
				$wheres_['start_time'] = date($format);
				$con_xufei_add = M('customer_xufei')->add($wheres_);
				if ($con_xufei_add) {
					M('customer_xufei')->commit();
				}

				if ($paysave && $addcom && $conadd && $con_xufei_add) {
					$r['rcode']=1;
					$r['msg'] = '信息审核成功';
					$r['data'] = $this->where(array('payrecord_id'=>$arrArr['id']))->find();
				}else{
					M('customer')->rollback();
					M('contract')->rollback();
					$this->rollback();
					M('customer_xufei')->rollback();
					$r['msg'] = '请重新评论审核信息';
				}
			}
		}else{
			$r['msg']='查账信息不存在';
		}
		return $r;
	}

	// 退款审核通过
	public function ver_fed($arrArr = array('audit_msg'=>NULL,'pay_way'=>NULL,'payrecord_id'=>NULL),$format='Y-m-d H:i:s'){
		$r = array('data'=>NULL,'msg'=>'','rcode'=>0);
		$arr = $wheres = array();
		$Reuser = M('user') ->where(array('user_id'=> $this->Guid()))->find();
		$contract = M('contract')->join('payrecord ON payrecord.contract_id = contract.contract_id')
								 ->where(array('payrecord.payrecord_id'=>$arrArr['payrecord_id']))
								 ->find();
		$arr['auditor']=$this->Guid();
		$arr['audit_sector']=$Reuser['sector_id'];
		$arr['audit_role'] = $Reuser['role_id'];
		$arr['payrecord_id'] = $arrArr['payrecord_id'];
		$arr['audit_msg'] = $arrArr['audit_msg'];
		$arr['pay_way'] = $arrArr['pay_way'];
		$arr['audit_time']=date($format);
		$arr['pay_time']=date($format);
		$arr['audit_res'] = 1;
		$wheres['state'] = 3;
		$wheres['contract_id'] = $contract['contract_id'];
		$paysave = $this->save($arr);
		$payconsave = M('contract')->save($wheres);
		if ($paysave && $payconsave) {
				$r['rcode']=1;
				$r['msg'] = 'ok';
				$r['data'] = $this->where(array('payrecord_id'=>$arrArr['payrecord_id']))->find();
		}else{
			$r['msg'] = '请重新评论审核信息';
		}
		return $r;
	}

   // 获得用户岗位及其下属
   public function getrole(){
      $uinfo = UserModel::loadFromSession();
      $roleid = $uinfo['role_id'];
      $role =  M('role');
      $data = $role->select();
      return $this->getroles($data,$roleid);
   }


   public function getroles($data,$roleid){
      static $attid=array();
      $attid[]=$roleid;
      foreach ($data as $key => $value) {
         if ($value['parent_role'] == $roleid && $value['role_id'] != $roleid) {//找子类
            $attid[]=$value['role_id'];
            $this->getroles($data,$value['role_id']);
         }
      }
      return array_unique($attid);
   }

  // 获得所属部门以及其下属部门
   public function getchild(){
      $uinfo = UserModel::loadFromSession();
      $sectorid = $uinfo['sector_id'];
      $res =  M('sector')->where('sector_id = '.$sectorid)->find();
      if ($res['parent_sector'] == 1 ) {//齐总位置
        $data = M('sector')->select();
        return $this->get_childids($data,$sectorid);
      }elseif($sectorid!=1 && $res['parent_sector'] != 1){//总监及经理位置
        $data = M('sector')->select();
        return $this->get_childids_($data,$sectorid);
      }elseif ($sectorid == 1) {//admin位置
        $data = M('sector')->select();
        return $this->get_childids_admin($data,$sectorid);
        // return $sectorid;
      }
   }

   // 以总监、经理、及员工的身份进入
   public function get_childids_($data,$sectorid){
      static $arrid=array();
      $arrid[]=$sectorid;
      foreach ($data as $key => $value) {
         if ($value['parent_sector'] == $sectorid && $value['sector_id'] !=  $sectorid) {//找子类
            $arrid[]=$value['sector_id'];
            $this->get_childids_($data,$value['sector_id']);
         }
      }
      if (count($arrid) == 1) {
      	return array_unique($arrid);
      }else
      	return array_slice(array_unique($arrid),1);
   }

   // 以齐总的身份进入
   public function get_childids($data,$sectorid){
      static $arrid=array();
      if (empty($arrid)) $arrid[]=$sectorid;
      foreach ($data as $key => $value) {
         if ($value['parent_sector'] == $sectorid && $value['sector_id'] !=  $sectorid) {//找子类
         	if ($arrid[0] != $value['parent_sector']) {
            	$arrid[]=$value['sector_id'];
         	}
            $this->get_childids($data,$value['sector_id']);
         }
      }
      return array_slice(array_unique($arrid),1);
   }

   // 以admin的身份进入
   public function get_childids_admin($data,$sectorid){
      static $arrid=array();
      static $secId=array();
      if (empty($arrid)) $arrid[]=$sectorid;
      foreach ($data as $key => $value) {
         if ($value['parent_sector'] == $sectorid && $value['sector_id'] !=  $sectorid) {//找子类
         	if ($arrid[0] != $value['parent_sector']) {
         		if (!in_array($value['parent_sector'], $secId)) {
            		$arrid[]=$value['sector_id'];
         		}
         	}else{
         		$secId[]=$value['sector_id'];
         	}
            $this->get_childids_admin($data,$value['sector_id']);
         }
      }
      return array_slice(array_unique($arrid),1);
            // return $arrid;
   }

    //获取自己以及下属业绩总和
	public function Choose_role($arrTrans = array('trans'=>NULL,'start_time'=>NULL,'end_time'=>NULL)){
		$arr = $att = $wheres = array();
		if ($arrTrans['start_time'] && !$arrTrans['end_time']){
			$wheres['payrecord.pay_time'] = array('EGT',$arrTrans['start_time']);
		}
		if ($arrTrans['end_time'] && !$arrTrans['start_time']) {
			$wheres['payrecord.pay_time'] = array('ElT',$arrTrans['end_time']);
		}
		if ($arrTrans['start_time'] && $arrTrans['end_time']) {
			$wheres['payrecord.pay_time'] = array('exp','>= \''.$arrTrans['start_time'].'\' AND payrecord.pay_time <= \''.$arrTrans['end_time'].'\'');
		}
		$wheres['payrecord.audit_res'] = 1;
		$salarys = 0;
		$user = D('user')->field('nickname,user_id')
						 ->where(array('role_id'=>array('IN',$arrTrans['trans'])))
						 ->select();
		foreach ($user as &$v_user) {
			$att[] = $v_user['user_id'];
		}
		$wheres['customer_xufei.user_id'] = array('IN',$att);
		$payrecord = D('payrecord') ->join('customer_xufei ON customer_xufei.customer_id= payrecord.customer_id')
									->where($wheres)
									->select();
		foreach ($user as &$v) {
			$arr['sector_name'][] = $v['nickname'];
			if (!empty($payrecord)) {
				foreach ($payrecord as &$v_) {
					if ($v['user_id'] == $v_['user_id']) {
						$salarys+=$v_['pay_mount'];
					}
				}
				$arr['pay_mount'][]=$salarys;//循环一次之后将金额赋给续费人
				$salarys = 0;
			}else
				$arr['pay_mount'][] = 0;
		}
		$arrARR = array();
		for ($i=0; $i < count($arr['pay_mount']); $i++) {
			 $arrARR[$i]['sector_name'] = $arr['sector_name'][$i];
			 $arrARR[$i]['pay_mount'] = $arr['pay_mount'][$i];
		}
		return $arrARR;
	}

	// 获取部门业绩统计
	public function choose_sector($arrSector=array('trans'=>NULL,'start_time'=>NULL,'end_time'=>NULL)){
		$arr = $att = $wheres = array();
		if ($arrSector['start_time'] && !$arrSector['end_time']){
			$wheres['payrecord.pay_time'] = array('EGT',$arrSector['start_time']);
		}
		if ($arrSector['end_time'] && !$arrSector['start_time']) {
			$wheres['payrecord.pay_time'] = array('ElT',$arrSector['end_time']);
		}
		if ($arrSector['start_time'] && $arrSector['end_time']) {
			if ($arrSector['start_time'] > $arrSector['end_time']) break;
			$wheres['payrecord.pay_time'] = array('exp','>= \''.$arrSector['start_time'].'\' AND payrecord.pay_time <= \''.$arrSector['end_time'].'\'');
		}
		$wheres['payrecord.audit_res'] = 1;
		static $arrStatic = array('sector_name'=>array(),'user_id'=>array());
		//获取部门名字和部门所属员工
		$userBack = M('user')
							 ->join('sector ON sector.sector_id = user.sector_id')
							 ->where(array('sector.sector_id'=>array('IN',$arrSector['trans'])))
							 ->select();
		for ($i=0; $i <count($arrSector['trans']) ; $i++) {
			unset($arrStatic['user_id']);
			foreach ($userBack as $k => $v) {
				if ($v['sector_id'] == $arrSector['trans'][$i]) {
					$arrStatic['user_id'][] = $v['user_id'];
					if (!in_array($v['sector_name'], $arrStatic['sector_name'])) {
						$arrStatic['sector_name'][] = $v['sector_name'];
						$arr['sector_name'][] = $v['sector_name'];
					}
				}
			}
			$wheres['customer_xufei.user_id'] = array('IN',$arrStatic['user_id']);
			if ($arrStatic['user_id']) {//如果不存在，就说明user表中不存在战队的员工，战队名字和业绩都不会记录
				$count = D('payrecord') ->join('customer_xufei ON customer_xufei.customer_id= payrecord.customer_id')
											->where($wheres)
											->sum(pay_mount);
				if (empty($count)) {
					$arr['pay_mount'][] = 0;
				}else{
					$arr['pay_mount'][] = $count;
				}
			}

		}
		$arrARR_S = array();
		for ($i=0; $i < count($arr['pay_mount']); $i++) {
			 $arrARR_S[$i]['sector_name'] = $arr['sector_name'][$i];
			 $arrARR_S[$i]['pay_mount'] = $arr['pay_mount'][$i];
		}
		return $arrARR_S;
	}
}
?>